package edu.hdu.android.sudoku;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;


import androidx.annotation.Nullable;

import java.util.Objects;

public class DrawView extends View {

    private static final String TAG = "DrawView";

    private static Game game;
    int selectedX;
    int selectedY;

    private float width;
    private float height;


    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        game = new Game();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.height = h/9f;
        this.width = w/9f;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint background = new Paint();
        Paint red = new Paint();
        Paint blue = new Paint();
        Paint text = new Paint();
        background.setColor(Color.argb(53,0,255,255));
        background.setStyle(Paint.Style.FILL);
        red.setColor(Color.RED);
        blue.setColor(Color.BLUE);
        //绘制背景
        canvas.drawRect(0, 0, getWidth(), getHeight(), background);
        //绘制网格线
        for (int i=1; i<9; i++) {
            canvas.drawLine(0, i*height, getWidth(), i*height, blue);
            canvas.drawLine(i*width, 0, i*width, getHeight(), blue);
            if (i%3 == 0) {
                canvas.drawLine(0, i*height, getWidth(), i*height,
                        red);
                canvas.drawLine(i*width, 0, i*width, getHeight(),
                        red);
            }
        }
        //绘制数字
        text.setTextSize(height*0.75f);
        text.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fm = text.getFontMetrics();
        float x = width/2;
        float y = height/1.3f;
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                if (game.fin[j*9+i] != '0') {
                    text.setColor(Color.RED);
                    canvas.drawText(game.getNumString(i ,j), i*width+x, j*height+y,
                            text);
                } else {
                    text.setColor(Color.BLACK);
                    canvas.drawText(game.getNumString(i ,j), i*width+x, j*height+y,
                            text);
                }
            }
        }
        super.onDraw(canvas);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            return super.onTouchEvent(event);
        }
        selectedX = (int) (event.getX()/width);
        selectedY = (int) (event.getY()/height);

        int[] used = game.getUsedNum(selectedX, selectedY);

        KeyDialog keyDialog = new KeyDialog(getContext(), this, used);
        WindowManager.LayoutParams lp = Objects.requireNonNull(keyDialog.getWindow())
                .getAttributes();
        lp.alpha=0.9f;
        keyDialog.getWindow().setAttributes(lp);
        keyDialog.show();
        return true;
    }

    public void setSelectedNum(int num) {
        if (game.setNumIfValid(selectedX, selectedY, num)) {
            invalidate();
        }
    }
}
